<template>
  <div class="app-container">
    <div class="filter-container">
      <el-tabs v-model="activeName" @tab-click="handleClickTab">
        <el-tab-pane label="轮播图" name="1"></el-tab-pane>
        <el-tab-pane label="广告图" name="2"></el-tab-pane>
        <el-tab-pane label="滚动通知" name="3"></el-tab-pane>
      </el-tabs>

      <br>
      <el-button-group>
        <el-button @click="openChoose" class="filter-item" type="primary" size="mini"
                   icon="el-icon-edit">
          添加楼层数据
        </el-button>
        <el-button size="mini" class="filter-item" type="danger" icon="el-icon-delete" @click="handleDelete">删除
        </el-button>
      </el-button-group>
    </div>
    <el-table
        :key="tableKey"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column label="通知内容" align="center" prop="cateName" v-if="listQuery.type===3">
        <template slot-scope="scope">
          <span>{{ scope.row.rollingNotify }}</span>
        </template>
      </el-table-column>
      <el-table-column label="轮播图" align="center" prop="cateName" v-if="listQuery.type===1">
        <template slot-scope="scope">
          <el-image
              style="height: 30px"
              :src="scope.row.swiperUrl+'?'+$globalImageProcessingSuffix"
              :preview-src-list="[scope.row.swiperUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="广告图" align="center" prop="cateName" v-if="listQuery.type===2">
        <template slot-scope="scope">
          <el-image
              style="height: 30px"
              :src="scope.row.advertisingUrl+'?'+$globalImageProcessingSuffix"
              :preview-src-list="[scope.row.advertisingUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="跳转地址" align="center" prop="cateName" v-if="listQuery.type===1||listQuery.type===2">
        <template slot-scope="scope">
          <span>{{ scope.row.jumpUrl }}</span>
        </template>
      </el-table-column>


      <el-table-column :label="$t('table.actions')" class-name="small-padding" min-width="120px" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="openAddEditModal(scope.row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>

    <el-dialog
        :close-on-click-modal="false"
        title="选择要添加的楼层类型"
        :visible.sync="dialogVisibleChoose"
        width="30%">
      <el-form ref="form" label-width="100px">
        <el-form-item label="选择类型">
          <el-radio v-model="chooseType" :label="1">轮播图</el-radio>
          <el-radio v-model="chooseType" :label="2">广告图</el-radio>
          <el-radio v-model="chooseType" :label="3">滚动通知文本</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisibleChoose = false">取 消</el-button>
    <el-button type="primary" @click="goOpenAddEditModal">确 定</el-button>
    </span>
    </el-dialog>


    <el-dialog
        @close="cancel"
        :close-on-click-modal="false"
        :title="modalTitle"
        :visible.sync="dialogVisible"
        width="30%">
      <el-form ref="form" :model="form" label-width="100px" v-if="type===3">
        <el-alert
          class="warn-content"
          title=""
          type="success"
          :closable="false"
        >

          <div>提示：若需添加多条通知，多条之间用逗号隔开（英文输入法下的逗号）</div>

        </el-alert>
        <el-form-item label="滚动通知内容">
          <el-input v-model="form.rollingNotify"></el-input>
        </el-form-item>
      </el-form>

      <el-form ref="form" :model="form" label-width="100px" v-if="type===1">
        <el-form-item label="轮播图">
          <QiniuSingleUploadTable :pageType="'goods'" :type="1" :title="'图片导入'" v-model="form.swiperUrl"
                                  :value="form.swiperUrl" :isWatermark="false" :currentScope="{}"/>
          <!--          <Upload v-model="postForm.mainImg" />-->
          <ImageSource style="margin-top: -5px;" :mul="false" v-model="form.swiperUrl"/>
        </el-form-item>
        <el-form-item label="跳转地址">
          <el-input v-model="form.jumpUrl"></el-input>
        </el-form-item>
      </el-form>

      <el-form ref="form" :model="form" label-width="100px" v-if="type===2">
        <el-form-item label="广告图">
          <QiniuSingleUploadTable :pageType="'goods'" :type="1" :title="'图片导入'" v-model="form.advertisingUrl"
                                  :value="form.advertisingUrl" :isWatermark="false" :currentScope="{}"/>
          <!--          <Upload v-model="postForm.mainImg" />-->
          <ImageSource style="margin-top: -5px;" :mul="false" v-model="form.advertisingUrl"/>
        </el-form-item>
        <el-form-item label="跳转地址">
          <el-input v-model="form.jumpUrl"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible=false">取 消</el-button>
    <el-button type="primary" @click="goToAdd">确 定</el-button>
    </span>
    </el-dialog>

  </div>
</template>

<script>
import {getListHomeFloor, editSubmitHomeFloor, deleteHomeFloor} from '@/api/homeFloor'
import waves from '@/directive/waves' // Waves directive
import {parseTime, renderTime} from '@/utils'
import Pagination from '@/components/Pagination'
import QiniuSingleUploadTable from "../../../components/Upload/qiniuSingleUploadTable.vue";
import ImageSource from "../../../components/Upload/imageSource.vue"; // Secondary package based on el-pagination

const typeOptions = [
  {id: 1, name: '轮播图'},
  {id: 2, name: '广告图'},
  {id: 3, name: '滚动通知文本'}
]

// arr to obj ,such as { CN : "China", US : "USA" }
const typeKeyValue = typeOptions.reduce((acc, cur) => {
  acc[cur.id] = cur.name
  return acc
}, {})

export default {
  name: 'articleTable',
  components: {ImageSource, QiniuSingleUploadTable, Pagination},
  directives: {waves},
  filters: {
    typeFilter(status) {
      return typeKeyValue[status]
    }
  },
  data() {
    return {
      activeName: "1",
      chooseType: 1,
      id: null,
      type: null,
      form: {},
      dialogVisible: false,
      dialogVisibleChoose: false,
      modalTitle: "",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20
      },
      multipleSelection: [],
      typeOptions,

    }
  },
  created() {
    this.listQuery.type = 1
    this.getList()
  },
  methods: {
    handleClickTab(tab, event) {
      this.listQuery.page = 1
      this.listQuery.pageSize = 20
      this.activeName = tab.name+""
      if (tab.name == '1') {
        this.listQuery.type = 1
      } else if (tab.name == '2') {
        this.listQuery.type = 2
      } else if (tab.name == '3') {
        this.listQuery.type = 3
      }
      this.getList()
    },
    cancel() {
      this.dialogVisible = false
      this.getList()
    },
    goToAdd() {
      let formData = this.form
      let errorType = null
      let message = null
      if (formData.type===1){
        if (!formData.swiperUrl){
          errorType=1
          message= "轮播图不能为空"
        }
      }else if (formData.type===2){
        if (!formData.advertisingUrl){
          errorType=2
          message= "广告图不能为空"
        }
      }else if (formData.type===3){
        if (!formData.rollingNotify){
          errorType=3
          message= "滚动通知内容不能为空"
        }
      }
      if (errorType){
        this.$notify({
          title: '失败',
          message: message,
          type: 'error',
          duration: 2000
        })
        return
      }

      editSubmitHomeFloor(formData).then(() => {
        this.dialogVisible = false
        this.dialogVisibleChoose = false
        this.listQuery.type = this.form.type
        this.activeName = this.form.type+""
        this.$notify({
          title: '成功',
          message: '商品参数添加成功',
          type: 'success',
          duration: 2000
        })


      })
    },
    openChoose() {
      this.chooseType = 1
      this.dialogVisibleChoose = true;
    },
    goOpenAddEditModal() {
      let data = {
        type: this.chooseType
      }
      this.form = {}
      this.openAddEditModal(data)
    },
    openAddEditModal(data) {
      this.id = data.id
      this.type = data.type
      if (data.type === 1) {
        this.modalTitle = "轮播图"
      } else if (data.type === 2) {
        this.modalTitle = "广告图"
      } else if (data.type === 3) {
        this.modalTitle = "滚动通知文本"
      }
      if (this.id) {
        this.form = data
      }
      this.form.type = data.type
      this.dialogVisible = true

    },
    getList() {
      this.listLoading = true
      getListHomeFloor(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelete() {
      const delVals = this.multipleSelection;
      if (!delVals || delVals.length <= 0) {
        this.$message({
          message: '请至少选择一条数据',
          type: 'error',
          duration: 2000
        })
        return;
      }
      this.$confirm('您确定要删除这些记录?', '提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let values = [];
        delVals.forEach(val => {
          values.push(val.id);
        })
        deleteHomeFloor(values.join(',')).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
          .catch(err => {
            console.error(err)
          })
    },
  }
}
</script>
